{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 向量投影\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "该代码的数学核心是计算两个向量之间的 **点积**、**模长**、**标量投影** 以及 **向量投影**，从而分析它们在彼此方向上的投影关系。\n",
    "\n",
    "---\n",
    "\n",
    "### **1. 向量与点积**\n",
    "已定义的向量：\n",
    "$$\n",
    "\\mathbf{a} = \\begin{bmatrix} 3 \\\\ 4 \\end{bmatrix}, \\quad \\mathbf{e}_1 = \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix}\n",
    "$$\n",
    "点积（内积）的定义为：\n",
    "$$\n",
    "\\mathbf{a} \\cdot \\mathbf{b} = \\sum_{i=1}^{n} a_i b_i\n",
    "$$\n",
    "也可以用几何定义：\n",
    "$$\n",
    "\\mathbf{a} \\cdot \\mathbf{b} = \\|\\mathbf{a}\\| \\|\\mathbf{b}\\| \\cos\\theta\n",
    "$$\n",
    "其中 $\\theta$ 是两个向量之间的夹角。\n",
    "\n",
    "代码计算：\n",
    "$$\n",
    "\\mathbf{a} \\cdot \\mathbf{e}_1 = (3 \\times 1) + (4 \\times 0) = 3\n",
    "$$\n",
    "$$\n",
    "\\mathbf{e}_1 \\cdot \\mathbf{a} = (1 \\times 3) + (0 \\times 4) = 3\n",
    "$$\n",
    "由于点积满足交换律，即 $\\mathbf{a} \\cdot \\mathbf{b} = \\mathbf{b} \\cdot \\mathbf{a}$，所以两者结果相同。\n",
    "\n",
    "---\n",
    "\n",
    "### **2. 向量的模长**\n",
    "向量的欧几里得范数（模长）定义为：\n",
    "$$\n",
    "\\|\\mathbf{a}\\| = \\sqrt{\\sum_{i=1}^{n} a_i^2}\n",
    "$$\n",
    "计算 $\\mathbf{a}$ 的模：\n",
    "$$\n",
    "\\|\\mathbf{a}\\| = \\sqrt{3^2 + 4^2} = \\sqrt{9 + 16} = \\sqrt{25} = 5\n",
    "$$\n",
    "计算 $\\mathbf{e}_1$ 的模：\n",
    "$$\n",
    "\\|\\mathbf{e}_1\\| = \\sqrt{1^2 + 0^2} = \\sqrt{1} = 1\n",
    "$$\n",
    "\n",
    "---\n",
    "\n",
    "### **3. 标量投影**\n",
    "标量投影指的是一个向量在另一个向量上的投影长度，计算公式如下：\n",
    "$$\n",
    "\\text{scalar-sproj}_{\\mathbf{b}} \\mathbf{a} = \\frac{\\mathbf{a} \\cdot \\mathbf{b}}{\\|\\mathbf{b}\\|}\n",
    "$$\n",
    "代入计算：\n",
    "$$\n",
    "\\text{scalar-proj}_{\\mathbf{e}_1} \\mathbf{a} = \\frac{3}{1} = 3\n",
    "$$\n",
    "$$\n",
    "\\text{scalar-proj}_{\\mathbf{a}} \\mathbf{e}_1 = \\frac{3}{5} = 0.6\n",
    "$$\n",
    "\n",
    "标量投影的几何意义：\n",
    "- $\\mathbf{a}$ 在 $\\mathbf{e}_1$ 上的标量投影 $3$ 代表 $\\mathbf{a}$ 在 $x$ 轴上的分量。\n",
    "- $\\mathbf{e}_1$ 在 $\\mathbf{a}$ 上的标量投影 $0.6$ 代表 $\\mathbf{e}_1$ 在 $\\mathbf{a}$ 方向的分量大小。\n",
    "\n",
    "---\n",
    "\n",
    "### **4. 向量投影**\n",
    "向量投影不仅包括长度，还保持方向，计算公式：\n",
    "$$\n",
    "\\text{proj}_{\\mathbf{b}} \\mathbf{a} = \\frac{\\mathbf{a} \\cdot \\mathbf{b}}{\\|\\mathbf{b}\\|^2} \\mathbf{b}\n",
    "$$\n",
    "计算 $\\mathbf{a}$ 在 $\\mathbf{e}_1$ 上的向量投影：\n",
    "$$\n",
    "\\text{proj}_{\\mathbf{e}_1} \\mathbf{a} = \\frac{3}{1^2} \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} = \\begin{bmatrix} 3 \\\\ 0 \\end{bmatrix}\n",
    "$$\n",
    "计算 $\\mathbf{e}_1$ 在 $\\mathbf{a}$ 上的向量投影：\n",
    "$$\n",
    "\\text{proj}_{\\mathbf{a}} \\mathbf{e}_1 = \\frac{3}{5^2} \\begin{bmatrix} 3 \\\\ 4 \\end{bmatrix} = \\frac{3}{25} \\begin{bmatrix} 3 \\\\ 4 \\end{bmatrix} = \\begin{bmatrix} 0.36 \\\\ 0.48 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "---\n",
    "\n",
    "### **5. 结论**\n",
    "- **标量投影** 计算向量的投影长度，忽略方向性。\n",
    "- **向量投影** 计算向量在另一向量方向上的投影，保持方向信息。\n",
    "- 向量 $\\mathbf{a}$ 在 $\\mathbf{e}_1$ 上的投影是 $\\begin{bmatrix} 3 \\\\ 0 \\end{bmatrix}$，表示 $\\mathbf{a}$ 的 $x$ 轴分量。\n",
    "- 向量 $\\mathbf{e}_1$ 在 $\\mathbf{a}$ 上的投影是 $\\begin{bmatrix} 0.36 \\\\ 0.48 \\end{bmatrix}$，表示单位基向量 $\\mathbf{e}_1$ 在 $\\mathbf{a}$ 方向上的投影分量。\n",
    "\n",
    "这些计算在 **计算机图形学、信号处理、机器学习和物理学** 等领域都有广泛应用，尤其是在 **正交分解、最小二乘回归、特征向量分析** 等问题中。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d8e7c62-8f8c-472c-9ec8-7668dffcfb88",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6032b9c2-b75c-4c64-a4dd-186fdaec8e3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ffc4a51-ef9a-49d3-abf4-cceb1f068b83",
   "metadata": {},
   "source": [
    "## 定义向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "90f57a76-5910-4ef7-916c-2e0e9a195443",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([3, 4])\n",
    "e1 = np.array([1, 0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78eca474-e4bb-4997-97e9-d7b5103eaa17",
   "metadata": {},
   "source": [
    "## 计算点积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f6a27f63-5aa0-4eea-af0a-e61bc9ba921b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dot_a_e1 = np.dot(a, e1)\n",
    "dot_a_e1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c431d449-36d1-484c-a592-f8968da55aac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dot_e1_a = np.dot(e1, a)\n",
    "dot_e1_a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "361cdced-92c8-4e26-a92b-87295cb82752",
   "metadata": {},
   "source": [
    "## 计算向量模长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "03094efa-37b6-4e38-baae-be93934e74d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "norm_a = np.linalg.norm(a)\n",
    "norm_a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "35a19617-11cd-405e-9656-72375367dc74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "norm_e1 = np.linalg.norm(e1)\n",
    "norm_e1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40f1b863-4be0-494d-bb42-ee6e5f5f2d83",
   "metadata": {},
   "source": [
    "## 计算标量投影"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "054e6463-d9e8-4292-91f0-276a597f9790",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scalar_proj_a_on_e1 = dot_a_e1 / norm_e1  # a 在 e1 上的投影\n",
    "scalar_proj_a_on_e1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b06967e2-f8d9-4239-a18a-4781173055ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scalar_proj_e1_on_a = dot_e1_a / norm_a  # e1 在 a 上的投影\n",
    "scalar_proj_e1_on_a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c942fddf-eb04-46ce-89c6-f30a65c6833d",
   "metadata": {},
   "source": [
    "## 计算向量投影"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3bea69bd-8782-44e0-95b7-e6e1d188079c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3., 0.])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vector_proj_a_on_e1 = (dot_a_e1 / norm_e1**2) * e1  # 向量 a 在 e1 上的投影\n",
    "vector_proj_a_on_e1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9a7f82fb-0baf-45bc-931b-67e5c65e8ea6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.36, 0.48])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vector_proj_e1_on_a = (dot_e1_a / norm_a**2) * a  # 向量 e1 在 a 上的投影\n",
    "vector_proj_e1_on_a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b30c69fb-e50a-4e03-9adb-f13bc0f154f8",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
